package com.gj.demojuc.chap05;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 到计数器
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        //创建到计数器对象
        CountDownLatch countDownLatch = new CountDownLatch(6);
        //循环创建6个子线程分别执行各自业务
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "开始学习");
                try {
                    TimeUnit.SECONDS.sleep(new Random().nextInt(10));

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() +"结束学习");
                countDownLatch.countDown();
            },"同学" + i).start();
        }
        //主线程阻塞等待各个子线程结束
        countDownLatch.await(5,TimeUnit.SECONDS);
        System.out.println(Thread.currentThread().getName() + "班长关门");
    }
}
